<?php
	include_once('object/WordObject.php');
	include_once('object/Person.php');
	include_once('object/PublicGroup.php');
/**
 * User LiveWord Class
 */
	session_start();
	class liveWord {
		var $user_id;
		var $user_name;
	    /**
	     * Initial UserInformation And Create DataBase Connection
	     */	
		function liveWord(){
//create database initial
			mysql_connect("localhost", "root","3210021");
			mysql_select_db("im");
			mysql_query("SET NAMES UTF8");
			
//get user_id session
			$this->user_id = $_SESSION['uid'];
			$this->user_name = $_SESSION['user_name'];
			$this->avatar = $_SESSION['avatar'];
		}
	    /**
	     * getSelfInfo method
	     * @returns a array word content
	     */
	    public function getSelfInfo(){
	    	$sinfo = new Person();
	    	$sinfo->uid = $this->user_id;
	    	$sinfo->username = $this->user_name;
	    	$sinfo->avatar = $this->avatar;
	    	
	    	return $sinfo;
	    }
	    /**
	     * getUserWord method
	     * @returns a array word content
	     */
		public function getUserWord($sid){
			$w = array();

			$sql = "SELECT pms.message,pms.dateline,m.username FROM cdb_pms pms LEFT JOIN cdb_members m ON m.uid=pms.msgfromid WHERE pms.msgfromid=$sid and pms.msgtoid=$this->user_id and pms.delstatus=0 order by pms.pmid ASC LIMIT 0 , 20";

			$result = mysql_query($sql);
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$word = new WordObject();
					$word->date = gmdate("H:i:s",$row['dateline'] + 8 * 3600);
					$word->message = $row['message'];
					$word->username = $row['username'];
					
					$w[] = $word;
				}
			$sql ="UPDATE `cdb_pms` SET `delstatus`=2,new=0 WHERE `msgfromid`=$sid and `msgtoid`=$this->user_id and `delstatus`=0";		
			mysql_query("SET NAMES UTF8");
			$result = mysql_query($sql);
			
			mysql_close();
			
			return $w;
		}
	    /**
	     * sendWord method
	     * @returns a array word content
	     */
	    public function sendWord($msgtoid,$message){
			$time =time();
			$sql = "INSERT INTO `cdb_pms` ( `pmid` , `msgfrom` , `msgfromid` , `msgtoid` , `folder` , `new` , `subject` , `dateline` , `message` , `delstatus` )
VALUES (
NULL , '$this->user_name', '$this->user_id', '$msgtoid', 'inbox', '1', '', '$time', '$message', '0'
);";

			$result = mysql_query($sql);
			
			mysql_close();
			if($result){
				return "ok";
			}else {
				return "error";
			}
	    }
	    /**
	     * getUnReadList method
	     * @returns a array unread userid
	     */
		public function getUnReadList(){
			$sql = "SELECT pms.message, pms.dateline, m.username,m.invisible, pms.`msgfromid`
					FROM cdb_pms pms
					LEFT JOIN cdb_members m ON m.uid = pms.msgfromid
					WHERE (
					pms.msgfromid
					IN (
					
					SELECT DISTINCT `msgfromid`
					FROM cdb_pms
					WHERE `msgtoid` =$this->user_id
					AND `delstatus` =0
					)
					)
					AND pms.msgtoid =$this->user_id
					AND pms.delstatus =0
					ORDER BY pms.pmid ASC";

			$result = mysql_query($sql);
			$p = array();
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$word = new WordObject();
					$word->date = gmdate("H:i:s",$row['dateline'] + 8 * 3600);
					$word->message = $row['message'];
					$word->username = $row['username'];
					$word->data = $row['msgfromid'];
					
					$word->invisible = $row['invisible'];

					switch($word->invisible){
						case 0:
						$word->icon = "online";
						break;
						case 1:
						$word->icon = "hide";
						break;
						case 2:
						$word->icon = "busy";
						break;
						case 3:
						$word->icon = "leave";
						break;
						
						default:
						$word->icon = "online";
						break;
					}
					
					$sql1 = "SELECT `groupid` FROM `cdb_customgpmember` where `uid`=$word->data AND `buddyid`=$this->user_id";
					$result1 = mysql_query($sql1);
					$row1 = mysql_fetch_array($result1, MYSQL_ASSOC);
					
					if($row1['groupid'] == ""){
						$word->parent = -1;
					}else{
						$word->parent = $row1['groupid'];
					}
					
					$p[] = $word;
				}
			
			return $p;
		}   
	    /**
	     * sendGroupWord method
	     * @returns a array word content
	     */
	    public function sendGroupWord($msgtogid,$message){
			$time =time();
			$sql = "INSERT INTO `cdb_gms` ( `gmid` , `msgfrom` , `msgfromid` , `msgtogid` , `folder` , `new` , `subject` , `dateline` , `message` , `delstatus` )
VALUES (
NULL , '$this->user_name', '$this->user_id', '$msgtogid', 'inbox', '1', '', '$time', '$message', '0'
);";

			$result = mysql_query($sql);
			
			mysql_close();
			if($result){
				return "ok";
			}else {
				return "error";
			}
	    }
	    /**
	     * getGUserWord method
	     * @returns a array word content
	     */
		public function getGUserWord($gid){
			$sql ="SELECT `last_id` FROM `cdb_publicgmember` where `uid`=$this->user_id  AND `pgid`=$gid";

			$result = mysql_query($sql);
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$last_id = $row['last_id'];
			
			$w = array();

			$sql = "SELECT gms.gmid,gms.message,gms.dateline,m.username FROM cdb_gms gms LEFT JOIN cdb_members m ON m.uid=gms.msgfromid WHERE gms.msgtogid=$gid and gms.delstatus=0 and (gms.gmid >$last_id) and (gms.msgfromid !=$this->user_id) order by gms.gmid ASC";

			$result = mysql_query($sql);
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$word = new WordObject();
					$word->date = gmdate("H:i:s",$row['dateline'] + 8 * 3600);
					$word->message = $row['message'];
					$word->username = $row['username'];
					
					$w[] = $word;
					
					$last_id = $row['gmid'];
				}
				
			$sql = "UPDATE `cdb_publicgmember` SET `last_id` = '$last_id' WHERE `cdb_publicgmember`.`pgid` =$gid AND `cdb_publicgmember`.`uid` =$this->user_id LIMIT 1";
			$result = mysql_query($sql);
			
			mysql_close();
		
			return 	$w;
		}
	    /**
	     * getAllUserWord method
	     * @returns a array word content
	     */
		public function getAllUserWord($sid){
			$w = array();

			$sql = "SELECT pms.message,pms.dateline,m.username FROM cdb_pms pms LEFT JOIN cdb_members m ON m.uid=pms.msgfromid WHERE pms.msgfromid=$sid and pms.msgtoid=$this->user_id and pms.delstatus=2 order by pms.pmid DESC";

			$result = mysql_query($sql);
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$word = new WordObject();
					$word->date = gmdate("H:i:s",$row['dateline'] + 8 * 3600);
					$word->message = $row['message'];
					$word->username = $row['username'];
					
					$w[] = $word;
				}
			
			mysql_close();
			
			return $w;
		}
	    /**
	     * getGAllUserWord method
	     * @returns a array word content
	     */
		public function getGAllUserWord($gid){
			
			$w = array();

			$sql = "SELECT gms.gmid,gms.message,gms.dateline,m.username FROM cdb_gms gms LEFT JOIN cdb_members m ON m.uid=gms.msgfromid WHERE gms.msgtogid=$gid and gms.delstatus=0 and (gms.msgfromid !=$this->user_id) order by gms.gmid ASC";

			$result = mysql_query($sql);
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$word = new WordObject();
					$word->date = gmdate("H:i:s",$row['dateline'] + 8 * 3600);
					$word->message = $row['message'];
					$word->username = $row['username'];
					
					$w[] = $word;
					
					$last_id = $row['gmid'];
				}
				
			$sql = "UPDATE `cdb_publicgmember` SET `last_id` = '$last_id' WHERE `cdb_publicgmember`.`pgid` =$gid AND `cdb_publicgmember`.`uid` =$this->user_id LIMIT 1";
			$result = mysql_query($sql);
			
			mysql_close();
		
			return 	$w;
		}

	    /**
	     * getUnReadPGNum method
	     * @returns a array unread userid
	     */
		public function getUnReadPGNum(){
			$sql = "SELECT DISTINCT pg.p_gid,pg.`p_gname` FROM `cdb_publicgroup` pg LEFT JOIN cdb_publicgmember pgm  ON pg.p_gid=pgm.pgid  where pg.p_regman=$this->user_id OR pgm.uid =$this->user_id";
			$result = mysql_query($sql);
			
			$p = array();
			while($row =mysql_fetch_array($result,MYSQL_ASSOC)){
				$gid = $row['p_gid'];
				
				$gp = new PublicGroup();
					
				$gp->groupid = $row['p_gid'];
				$gp->groupname = $row['p_gname'];
				
				$sql1 ="SELECT `last_id` FROM `cdb_publicgmember` where `uid`=$this->user_id  AND `pgid`=$gid";
				$result1 = mysql_query($sql1);
				$row1 = mysql_fetch_array($result1, MYSQL_ASSOC);
				$last_id = $row1['last_id'];
				
				if($last_id == null){
					$last_id = 0;
				}
				
				$sql2 = "SELECT COUNT(gms.gmid) as gnum FROM cdb_gms gms WHERE gms.msgtogid=$gid and gms.delstatus=0 and (gms.gmid >$last_id) and (gms.msgfromid !=$this->user_id)";
				$result2 = mysql_query($sql2);
				$row2 = mysql_fetch_array($result2,MYSQLI_ASSOC);
				
				$gp->wordnum = $row2['gnum'];
				$p[] = $gp;
			}
			return $p;
		}   
	}
?>